<!--
 * @Author: your name
 * @Date: 2021-08-16 08:33:47
 * @LastEditTime: 2022-02-28 14:32:56
 * @LastEditors: jgg
 * @Description: In User Settings Edit
 * @FilePath: \yyzxwebd:\hsx\水文局设备运行巡检管理平台\web\src\views\equipmentManage\safeGuardList\add.vue
-->
<template>
    <el-dialog 
        :title="'设置权限'" 
        :model-value="visible"
        :destroy-on-close="true"
        width="600px"
        :before-close="onCancel"
    >
        <!-- fullscreen -->
        <el-tree
            style="height:400px;overflow:auto;"
            ref="permissionTree"
            v-loading="loading"
            :default-expand-all="true"
            :data="permissions"
            show-checkbox
            node-key="id"
            :props="{label: 'name', children: 'childList'}">
        </el-tree>
        <template #footer>
            <span class="dialog-footer">
                <el-button type="primary" @click="onSubmit" :loading="loading">保 存</el-button>
                <el-button @click="onCancel">取 消</el-button>
            </span>
        </template>
    </el-dialog>
</template>
<script>
import { formatTimestamp } from '@/utils/common'
import userApi from '@/api/user'
export default {
    emits: ['onChange'],
    components:{
    },
    props:{
        visible:{
            default:false,
            type: Boolean
        },
        type:{
            default:'add',
            type: String
        },
        item:{
            default:()=>{
                return {}
            },
            type: Object
        }
    },
    data(){
        return {
            loading:false,
            form:{
                status:'0'
                // maintenancePersonId:[1,2,3]
            },
            rules:{
                name: [
                    { required: true, message: '请输入名称', trigger: ['change','blur'], }
                ],
            },
            // 下拉数据
            comboList:[],
        }
    },
    watch:{
        visible(newValue){
            if(newValue){
                this.$nextTick(()=>{
                    this.$refs['form'].resetFields()
                })
                this.getlistCombobox()
                if(this.type == 'edit'){
                    this.form = JSON.parse(JSON.stringify(this.item))
                }
            }else{
                this.$refs['form'].resetFields()
                this.form = {}
                this.comboList = []
            }
        }
    },
    created(){
    },
    methods:{
        // 获取下拉数据
        getlistCombobox(){//部门
            userApi.getlistCombobox().then((res)=>{
                this.comboList = res.data[0].children || []
            }).catch((err)=>{
                this.$message.error(err.message)
            })
        },
        onSubmit(){
            this.$refs['form'].validate((valid) => {
                if (valid) {
                        this.loading = true
                        if(this.type == 'add'){
                            userApi.deptSave({
                                ...this.form,
                                parentId:1
                            }).then((res)=>{
                                this.loading = false
                                this.$emit('onChange',{bool:false,refresh:true})
                            }).catch((err)=>{
                                this.loading = false
                                this.$message.error(err.message)
                            })
                        }else if (this.type == 'edit'){
                            userApi.deptUpdate(this.form).then((res)=>{
                                this.loading = false
                                this.$emit('onChange',{bool:false,refresh:true})
                            }).catch((err)=>{
                                this.loading = false
                                this.$message.error(err.message)
                            })
                        }
                        
                } else {
                    return false
                }
            })
        },
        // 递归寻找被选中的节点
        findSelectKey(data) {
            if (data.length === 0) return
            data.map(item => {
                // 如果选中且没有子节点 直接push
                if(item.selected) {
                    if(item.childList.length === 0) {
                        this.checkedKey.push(item.id)
                    } else {
                        this.findSelectKey(item.childList)
                    }
                }
            })
        },
        // 查询角色权限
        handleGetData() {
            this.loading = true
            this.checkedKey = []
            roleApi.getRoleMenu(this.id)
                .then(res => {
                    this.permissions = res.data
                    this.findSelectKey(res.data)
                    this.$refs.permissionTree.setCheckedKeys(this.checkedKey, true)
                    this.loading = false
                })
                .catch(err => {
                    this.loading = false
                    this.$message.error(err.message)
                })
        },
        // 设置权限
        handleComfirm() {
            this.btnLoading = true
            this.disabled = true
            let halfSelectKey = this.$refs.permissionTree.getHalfCheckedKeys()
            let selectKey = this.$refs.permissionTree.getCheckedKeys()
            let selectAll = [...halfSelectKey,...selectKey]
            
            roleApi.updateRoleMenu({id: this.id, menuIds: selectAll}).then(res => {
                this.$message.success(res.message)
                this.$emit('update')
                this.$emit('close')
                this.btnLoading = false
                this.disabled = false
                
            }).catch((err) => {
                this.$message.error(err.message)
                this.disabled = false
                this.btnLoading = false
            })
        },
        onCancel(){
            this.$emit('onChange',{bool:false})
        }
    },
}
</script>
<style lang="scss" scoped>
    .form_height{
        max-height: 50vh;
        overflow-y: auto;
    }
    .width100{
        width: 100%;
    }
</style>